#include "Fighting.h"

using namespace cocos2d;
enum {
	kTagSprite2=1,
	kTagMap=2,
	kTagPos=3
};
Fighting::Fighting(void){
	//setIsTouchEnabled( true );
}
CCScene* Fighting::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        Fighting *layer = Fighting::node();
        CC_BREAK_IF(! layer);
		
        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool Fighting::init()
{
	
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////
        CC_BREAK_IF(! CCLayer::init());
		// Get window size 
        CCSize windowSize = CCDirector::sharedDirector()->getWinSize();
		//set the background
		CCTMXTiledMap* map= CCTMXTiledMap::tiledMapWithTMXFile("res/map.tmx");
		CCSize s = map->getContentSize();
	    addChild(map, 0,kTagMap);
		//the label to display the map pos
		CCLabelTTF* pos= CCLabelTTF::labelWithString("posizition", "Thonburi", 40);
		pos->setPosition(ccp(windowSize.width / 2, windowSize.height / 2));
        addChild(pos, 1,kTagPos);
		// load the object layer and get the coordinate of the spawnpoint
        CCTMXObjectGroup *objGroup = map->objectGroupNamed("objects");
        CCStringToStringDictionary* spawnPoint = objGroup->objectNamed("spawnpoint");
        float fSpawnPointX = spawnPoint->objectForKey("x")->toFloat();
        float fSpawnPointY = spawnPoint->objectForKey("y")->toFloat();
        // load player image texture
        CCTexture2D *pPlayerTexture = CCTextureCache::sharedTextureCache()->addImage("res/player.png");
        // create sprite instance
        CCSprite *pPlayer = CCSprite::spriteWithTexture(pPlayerTexture);
        pPlayer->setPosition(CCPoint(fSpawnPointX, fSpawnPointY));
		// add the player to the tile map
        map->addChild(pPlayer);
		
        bRet = true;
    } while (0);

    return bRet;
}

bool Fighting::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	
	return true;
}
void Fighting::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
	CCPoint touchLocation = touch->locationInView( touch->view() );	
	CCPoint prevLocation = touch->previousLocationInView( touch->view() );	
	
	touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
	prevLocation = CCDirector::sharedDirector()->convertToGL( prevLocation );
	
	CCPoint diff = ccpSub(touchLocation, prevLocation);
	
	CCNode *node = getChildByTag(kTagMap);
	CCPoint currentPos = node->getPosition();
	CCPoint actualPos=ccpAdd(currentPos,diff);
    node->setPosition( actualPos);
    //display the map center posizition
	/*CCLabelTTF* pos=(CCLabelTTF*)getChildByTag(kTagPos);
	char string[10] = {0};
	sprintf(string, "%f",actualPos.x );
	pos->setString(string);*/
	//node->setPosition( setViewpointCenter(actualPos));
}
void Fighting::onEnter()
{
	CCLayer::onEnter();
	CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
}

void Fighting::onExit()
{
	CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}
 CCPoint Fighting::setViewpointCenter(CCPoint position) {

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, (100*32) 
- winSize.width / 2);
y = MIN(y, (100*32) 
- winSize.height/2);
CCPoint actualPosition = ccp(x, y);

CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
return viewPoint;
}